<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      :title="article.title"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-show="loading" class="article-loading">加载中</van-loading>
    <!-- /加载中 loading -->
    <div class="error" v-if="is404">
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relativeTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="attentionBtn">
          {{ article.is_followed ? "取关" : "+ 关注" }}</van-button
        >
      </div>
      <div class="content">
        <div v-html="article.content">正文</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="hSwitchLike"
          >{{ article.attitude == 1 ? "取消点赞" : "点赞" }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
      </div>
    </div>
    <!-- /文章详情 -->
    <!-- 评论 -->
    <Comment />
  </div>
</template>

<script>
import { offAttention, attention } from '../../api/user'
import { getDetail, addLike, deleteLike } from '../../api/article'
import Comment from '../article/comment.vue'

export default {
  name: 'ArticleIndex',
  components: {
    Comment
  },
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      is404: false // 文章是否存在
    }
  },
  created () {
    this.loadDetail()
  },
  methods: {
    async loadDetail () {
      try {
        this.loading = true
        const res = await getDetail(this.$route.params.id)
        // console.log(res)
        this.article = res.data.data
        this.loading = false
        this.is404 = false
      } catch (err) {
        this.loading = false
        console.dir(err)
        if (err.response.status === 404) { this.is404 = true }
      }
    },
    async attentionBtn () {
      try {
        // 检查当前的状态
        const isFollowed = this.article.is_followed
        const userId = this.article.aut_id
        console.log(isFollowed)
        // 如果(is_followed === true) {
        //   取关
        // } else {
        //   加关
        // }
        if (isFollowed === true) {
          //   取关
          const res = offAttention(userId)
          console.log(res)
        } else {
          const res = await attention(userId)
          console.log(res)
        }
        // 更新视图
        //  1. 整体重发请求？（没有必要）
        //  2. 直接修改本地数据is_followed
        this.article.is_followed = !isFollowed

        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    // 点赞
    async hSwitchLike () {
      try {
        // 检查当前的状态
        const attitude = this.article.attitude
        const userId = this.article.art_id
        console.log(attitude)
        // 如果(is_followed === true) {
        //   取关
        // } else {
        //   加关
        // }
        if (attitude === 1) {
          //   取关
          const res = deleteLike(userId)
          this.article.attitude = -1
          console.log(res)
        } else if (attitude === -1) {
          const res = await addLike(userId)
          console.log(res)
          this.article.attitude = 1
        }
        // 更新视图
        //  1. 整体重发请求？（没有必要）
        //  2. 直接修改本地数据is_followed
        this.article.attitude = 1

        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
